package at.ac.tuwien.sepm.s2011.e0208329.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


import org.apache.log4j.Logger;

import at.ac.tuwien.sepm.s2011.e0208329.dao.DAOFactory;
import at.ac.tuwien.sepm.s2011.e0208329.dao.DAOFactoryImpl;
import at.ac.tuwien.sepm.s2011.e0208329.dao.DienstleistungDAO;
import at.ac.tuwien.sepm.s2011.e0208329.dao.OrderDAO;
import at.ac.tuwien.sepm.s2011.e0208329.dao.OrderDAOImpl;
import at.ac.tuwien.sepm.s2011.e0208329.dao.RechnungDAO;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Rechnung;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyBillService;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyBillServiceImpl;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyCarServices;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyCarServicesImpl;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyOrderService;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyOrderServiceImpl;

@SuppressWarnings("serial")
public class ShowBill extends JFrame {
	private JTextField[] fields;
	private String[] labels;
	private JPanel labelPanel;
	private JPanel fieldPanel;
	private MyBillService billSer;
	private MyCarServices service;
	private ArrayList<Dienstleistung> services;
	private int serNum;
	private static final Logger log = Logger.getLogger(ShowBill.class);

	public ShowBill(String name, int billID, String cusName, String cusAdress) {
		super(name);
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		service = MyCarServicesImpl.getInstance();
		billSer = MyBillServiceImpl.getInstance();
		this.setVisible(true);
		try {
			Map<Integer, Integer> serID_Num = billSer.getAllServiceIDForBill(billID);// get the all pairs of "ServiceID, Anzahl" with
			                                                                         // specified Bill ID
			serNum = serID_Num.size();
			labelPanel = new JPanel(new GridLayout(serNum + 3, 1));
			fieldPanel = new JPanel(new GridLayout(serNum + 3, 1));
			fields = new JTextField[serNum + 3];
			labels = new String[serNum + 3];
			labels[0] = "RechnungID : " + billID;
			labels[1] = "Kundenname : " + cusName;
			labels[2] = "Addresse : " + cusAdress;
			int labIndex = 3;		
			Iterator<Map.Entry<Integer, Integer>> entries = serID_Num.entrySet().iterator();
			while (entries.hasNext()) {
			    Map.Entry<Integer, Integer> entry = entries.next();
				labels[labIndex] = service.getSerName(entry.getKey().intValue()) + ":  " + entry.getValue();//get the Service's
				                                        // name through Service's ID and print as label
				labIndex++;
			}
			for (int i=0; i<labels.length; i++){// add labels to the panel
				JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
				labelPanel.add(lab);
			}

		} catch (SQLException e2) {
			log.error(e2.getMessage());
			JOptionPane.showMessageDialog(null, "Please see log file for more detail !", "Database error",
					JOptionPane.ERROR_MESSAGE);
		}
		this.getContentPane().add(labelPanel, BorderLayout.WEST);
		this.getContentPane().add(fieldPanel, BorderLayout.CENTER);
		this.pack();
	}

}

